import Vue from 'vue'
import Vuex from 'vuex'
import { message } from 'ant-design-vue'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: {
      id: '000',
      userName: 'offline',
      nickName: '未登录',
      email: '123@456.789',
      phone: '13345678925'
    },
    isLogin: false,
    clipBoard: {
      type: 'no', // 或cut或copy
      fileId: -1,
      parentId: -1
    },
    focusUserList: []
  },
  mutations: {
    setUserInfo(state, data) {
      state.userInfo = data
    },
    setIsLogin(state, data) {
      state.isLogin = data
    },
    setClipBoard(state, data) {
      state.clipBoard = data
    },
    setFocusUserList(state, data) {
      state.focusUserList = data
    }
  },
  actions: {
    setUserInfoAsync(context, data) {
      return new Promise((resolve, reject) => {
        Vue.prototype.$api.user
          .getUserInfo()
          .then((res) => {
            context.commit('setIsLogin', true)
            context.commit('setUserInfo', res.data)
            resolve(true)
          })
          .catch(() => {
            message.error('请登录')
            resolve(false)
          })
      })
    },
    setFocusUserListAsync(context, data) {
      Vue.prototype.$api.user.getFocusList().then((res) => {
        context.commit('setFocusUserList', res.data || [])
      })
    }
  },
  modules: {}
})
